* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Figure: Hand Out of Green Bracelets in Signals at 4 and 5 according to Timely Vaccinationù*
* **************************************************************************** *
/*
** PURPOSE:     Generate Figure:
                Hand Out of Green Bracelets in Signals at 4 and 5
                            according to Timely Vaccination


*/
* **************************************************************************** *
* **************************************************************************** *

	


	* Set format/style
	cap graph drop _all
	cd   "${Replicate_SocialSignals_Out}/colorschemes/"
	grstyle init bracelets,  replace
	grstyle set plain
	grstyle set imesh, horizontal compact
	grstyle set legend, nobox
	grstyle set inten 90: bar


	* --------------------------------------------------------------

	* ---------------------------------------------------------------------------- *
	* Prepare the data set
	use  "${Replicate_SocialSignals_dtaInter}/Listing_Implementation.dta", clear

	drop if type ==.

	keep if type==1
	drop if clinic==41
	drop if clinic_attend==0
	drop if immunization==0

	** Merge in start dates
	drop _merge
	sort  clinic
	merge m:m clinic using "${Replicate_SocialSignals_dtaInter}/MasterData_LaunchDates.dta", keep(3)

	egen    num_vacc = rowtotal(p3_listing p2_listing p1_listing m2_listing m1_listing bcg_listing)
	replace num_vacc = 5            if m1_listing==1


	save  "${Replicate_SocialSignals_dtaFinal}/Listing_HandOutGreenBracelets_S4_S5_prepared.dta", replace


	* ----------------------------------------------------------------------------
	* Penta 3 - Signal at 4 graph:

	use   "${Replicate_SocialSignals_dtaFinal}/Listing_HandOutGreenBracelets_S4_S5_prepared.dta", clear



	**keep if children were born shortly before or after launch 
	gen start = eligible - 45
	drop if dob<start


	* Do by age for Penta3 and Measles1
	* generate age categories
	rename  penta3_age age_penta3

	gen     age_categ_p3 = 122           if age_penta3<=122
	replace age_categ_p3 = 152           if age_penta3<=152 & age_penta3>122
	replace age_categ_p3 = 183           if age_penta3<=183 & age_penta3>152
	replace age_categ_p3 = 213           if age_penta3<=213 & age_penta3>183
	replace age_categ_p3 = 274           if age_penta3>213  & age_penta3!=.
	replace age_categ_p3 =.              if p3_listing==.

	lab var age_categ_p3 "Age at vaccination"
	lab def age_categ_p3 122 "by 4 months" 152 "4-5 mo" 183 "5-6 mo" 213 "6-7 mo" 274 "after 7 mo age"
	lab val age_categ_p3 age_categ_p3

	gen     yellow     = 1               if inlist(color_brac,"1")
	replace yellow     = 0               if inlist(color_brac,"2","2 3","1 2","3")

	gen     green      = 1               if inlist(color_brac,"2","1 2","2 3")
	replace green      = 0               if inlist(color_brac,"1","3")

	gen     nobracelet = 1               if inlist(color_brac,"3")
	replace nobracelet = 0               if inlist(color_brac,"1","1 2","2 3","2")

	keep if intervention_arm==3 & p3_listing==1

	summ    green yellow nobracelet
	local   Observations_S4  = trim("`: display %-9.0gc r(N)'")

	collapse (mean) mean_green  = green       ///
	mean_yellow = yellow      ///
	mean_nobra  = nobracelet, by(intervention_arm age_categ_p3)

	gsort  -mean_green mean_yellow

	graph bar  (asis) mean_green mean_yellow mean_nobra,   ///
	over(age_categ_p3,                                           ///
	relabel(122 "by 4 months" 152 "4 - 5 months" 183 "5 - 6 months" 213 "6 - 7 months" 274 "after 7 months age") ///
	sort(age_categ_p3))                                          ///
	bar(1, color(green%90))                                         ///
	bar(2, color(sandb%100))                                         ///
	bar(3, color(gs6)) stack ytick(0(0.1)1)  graphregion(color(none))   ///
	ytitle("Share of Children") title("Signal at 4") note("N =`Observations_S4'") b1title(Age at Vaccine 4)   ///
	legend(order(1 "Green bracelet" 2 "Yellow bracelet"  3 "No bracelet received") cols(3)) ///
	name(Arm3) 

	save "${Replicate_SocialSignals_dtaFinal}/listing_Arm3_AgeCat_GreenBracelets.dta", replace



	* ----------------------------------------------------------------------------
	* Measles 1 - Signal at 5 graph:

	use   "${Replicate_SocialSignals_dtaFinal}/Listing_HandOutGreenBracelets_S4_S5_prepared.dta", clear


	**keep if children were born shortly before or after launch // incl. all children that could have received Measles 1, green bracelet
	**90 days before first clinic hand out
	gen     start = eligible - 90
	drop if dob<start



	rename  measles1_age age_measles1
	gen     age_categ_m1 = 274    if age_measles1<=274
	replace age_categ_m1 = 335    if age_measles1<=335 & age_measles1>275
	replace age_categ_m1 = 365    if age_measles1>335
	replace age_categ_m1 =.       if m1_listing==.

	lab var age_categ_m1 "Age at vaccination"
	lab def age_categ_m1 274 "by 9 months" 335 "9-11 mo" 365 "after 11 mo age"
	lab val age_categ_m1 age_categ_m1


	gen     yellow     = 1        if inlist(color_brac,"1")
	replace yellow     = 0        if inlist(color_brac,"2","2 3","1 2","3")

	gen     green      = 1        if inlist(color_brac,"2","1 2","2 3")
	replace green      = 0        if inlist(color_brac,"1","3")

	gen     nobracelet = 1        if inlist(color_brac,"3")
	replace nobracelet = 0        if inlist(color_brac,"1","1 2","2 3","2")

	keep if intervention_arm==4 & m1_listing==1

	summ    green yellow nobracelet
	local   Observations_S5  = trim("`: display %-9.0gc r(N)'")

	collapse (mean) mean_green  = green       ///
	mean_yellow = yellow      ///
	mean_nobra  = nobracelet, by(intervention_arm age_categ_m1)

	gsort  -mean_green mean_yellow


	graph bar (asis) mean_green mean_yellow mean_nobra,                           ///
	over(age_categ_m1, sort(age_categ_m1)) stack                 ///
	bar(1, color(green))                                         ///
	bar(2, color(sandb))                                         ///
	bar(3, color(gs6))                                          ///
	ytick(0(0.1)1)  ytitle("") title("Signal at 5") b1title("Age at Vaccine 5") ///
	note("N = `Observations_S5'")   graphregion(color(none))                   ///
	legend(order(1 "Green bracelet" 2 "Yellow bracelet"  3 "No bracelet received") cols(3))  ///
	name(Arm4)

	save "${Replicate_SocialSignals_dtaFinal}/listing_Arm4_AgeCat_GreenBracelets.dta", replace




	* ----------------------------------------------------------------------------
	* Green Bracelet in Signal at 4 & 5

	grc1leg  Arm3 Arm4, ///
	legendfrom(Arm4) graphregion(color(white))
	graph export "${Replicate_SocialSignals_AFigures}/Figure_GreenBracelets_Timley_S4S5.jpg", replace





* ******************************************************************************
* End of the Dofile !!!
* ******************************************************************************
